local jimou = fk.CreateSkill{
  name = "lingling__jimou",
}

Fk:loadTranslationTable{
  ["lingling__jimou"] = "急谋",
  [":lingling__jimou"] = "出牌阶段限一次，你可以视为对一名其他角色依次使用两张目标为唯一其他角色的牌名不同的普通锦囊牌，结算后其获得X张【无懈可击】（X为生效牌数），且其下回合手牌上限-1。 ",

  ["#lingling__jimou"] = "急谋：视为对一名其他角色依次使用两张锦囊牌，令其获得无懈可击且手牌上限-1",

  ["#lingling__jimou-use"] = "急谋：再视为对 %dest 使用一张锦囊",

  ["@@lingling__jimou"] = "急谋",
}

local U = require "packages/utility/utility"

jimou:addAcquireEffect(function(self, player, is_start)
  local room = player.room
  local cards = table.filter(U.getUniversalCards(room, "t"), function(id)
    local card = Fk:getCardById(id)
    return not card.multiple_targets and card.skill:getMinTargetNum(player) > 0
  end)
  room:setPlayerMark(player, self.name, cards)
end)

jimou:addEffect("active", {
  anim_type = "control",
  prompt = "#lingling__jimou",
  max_phase_use_time = 1,
  expand_pile =function (self, player)
    return player:getTableMark(jimou.name)
  end,
  card_num = 1,
  card_filter = function(self, player, to_select, selected)
    if #selected > 0 then return false end
    local mark = player:getTableMark(jimou.name)
    if table.contains(mark, to_select) then
      local name = Fk:getCardById(to_select).name
      local card = Fk:cloneCard(name)
      card.skillName = jimou.name
      return card.skill:canUse(player, card, { bypass_distances = true, bypass_times = true })
    end
  end,
  target_filter = function(self, player, to_select, selected, selected_cards)
    if #selected_cards == 0 then return end
    local card = Fk:cloneCard(Fk:getCardById(selected_cards[1]).name)
    card.skillName = jimou.name
    if #selected == 0 and to_select == player.id then return end
    return card.skill:targetFilter(player, to_select, selected, {}, card, { bypass_distances = true, bypass_times = true })
  end,
  feasible = function(self, player, selected, selected_cards)
    if #selected_cards == 0 then return end
    local card = Fk:cloneCard(Fk:getCardById(selected_cards[1]).name)
    card.skillName = jimou.name
    return card.skill:feasible(player, selected, {}, card)
  end,
  on_use = function(self, room, use)
    local player = use.from
    local target = use.tos[1]
    local card = Fk:cloneCard(Fk:getCardById(use.cards[1]).name)
    card.skillName = jimou.name
    room:useCard{
      from = player,
      tos = use.tos,
      card = card,
    }
    if target.dead then return end

    if player:isAlive() then
      local all_cards = table.filter(U.getUniversalCards(player.room, "t"), function(id)
        local trick = Fk:getCardById(id)
        return not trick.multiple_targets and trick.skill:getMinTargetNum(player) > 0
      end)
      local cards = table.filter(all_cards, function (id)
        local card2 = Fk:getCardById(id)
        return card2.name ~= card.name and card2.skill:canUse(player, card2, {must_targets = {target.id}})
      end)
      if #cards > 0 then
        local dat = room:askToUseRealCard(player, {
          pattern = cards,
          skill_name = jimou.name,
          prompt = "#lingling__jimou-use::" .. target.id,
          cancelable = false,
          skip = true,
          extra_data = {
            expand_pile = cards,
            bypass_times = true,
            must_targets = {target.id},
          }
        })
        if dat then
          local use2 = {
            card = Fk:cloneCard(dat.card.name),
            from = player,
            tos = dat.tos,
            extraUse = true,
          }
          use2.card.skillName = jimou.name
          room:useCard(use2)
        end
      end
    end
    if target.dead then return end

    local n = player:getMark("lingling__jimou-tmp")
    room:setPlayerMark(player, "lingling__jimou-tmp", 0)
    if n == 0 then return end
    local nulls = room:getCardsFromPileByRule("nullification", n, "allPiles")
    room:obtainCard(target, nulls, nil, fk.ReasonPrey, player, jimou.name)
    room:addPlayerMark(target, "@@lingling__jimou", 1)
  end,
})
jimou:addEffect(fk.CardEffectFinished, {
  is_delay_effect = true,
  mute = true,
  can_refresh = function(self, event, target, player, data)
    return data.from and data.from == player and table.contains(data.card.skillNames, jimou.name)
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(player, "lingling__jimou-tmp", 1)
  end,
})
jimou:addEffect(fk.TurnEnd, {
  is_delay_effect = true,
  mute = true,
  can_refresh = function(self, event, target, player, data)
    return target == player and player:getMark("@@lingling__jimou")
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@@lingling__jimou", 0)
  end,
})
jimou:addEffect("maxcards", {
  correct_func = function(self, player)
    local n = player:getMark("@@lingling__jimou")
    if n > 0 then
      return -n
    end
  end
})

return jimou
